// 封装axios
import Axios from 'axios'
import encConfig from './env'

import {
    Message
} from 'element-ui'
import router from '@/router'

// 创建axios的实例
const service = Axios.create({
    baseURL: encConfig.prod.baseURL,
    timeout: 10000
})
// 创建请求拦拦截器
service.interceptors.request.use(
    (config) => {
        console.log("config:",config.url);
        // nprogress.start()
        if (config.url !== '/Login') {
            const token = sessionStorage.getItem('token')
            token && (config.headers.Authorization = token)
        }
        return config
    }, error => {
        return Promise.reject(error)
    })
// 创建响应拦截器
service.interceptors.response.use(
    (res) => {
        // nprogress.done()
        console.log('res', res)
        const {
            meta: {
                msg,
                status: code
            },
            data: result
        } = res.data
        const successCodeArr = [200, 201, 204]
        if (successCodeArr.includes(code)) {
            Message({
                message: msg,
                type: 'success'
            })
            if (res.config.url === '/login') {
                sessionStorage.setItem('token', result.token)
            }
            return result
        }
        Message({
            message: msg,
            type: 'error'
        })

        if (msg === '无效token') {
            router.replace('/login')
        }

        return Promise.reject(msg)
    },
    (error) => {
        return Promise.reject(error)
    }
)

export default service